<%
'******************************************************************************
' Copyright (C) 2006 Google Inc.
'  
' Licensed under the Apache License, Version 2.0 (the "License");
' you may not use this file except in compliance with the License.
' You may obtain a copy of the License at
'  
'      http://www.apache.org/licenses/LICENSE-2.0
'  
' Unless required by applicable law or agreed to in writing, software
' distributed under the License is distributed on an "AS IS" BASIS,
' WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
' See the License for the specific language governing permissions and
' limitations under the License.
'******************************************************************************
%>
<!-- #include file="../library/googleglobal.asp" -->
<%

' Retrieve the XML received
Dim ResponseXml, biData, nIndex
biData = Request.BinaryRead(Request.TotalBytes)
For nIndex = 1 To LenB(biData)
	ResponseXml = ResponseXml & Chr(AscB(MidB(biData,nIndex,1)))
Next

LogMessage ResponseXml

Dim MyOrder
Set MyOrder = New Order

Dim ResponseNode, RootTag
Set ResponseNode = GetRootNode(ResponseXml)
RootTag = ResponseNode.Tagname

Select Case RootTag
	Case "new-order-notification"
		ProcessNewOrderNotification ResponseXml
	Case "order-state-change-notification"
		ProcessOrderStateChangeNotification ResponseXml
	Case "risk-information-notification"
		ProcessRiskInformationNotification ResponseXml
	Case "charge-amount-notification"
		ProcessChargeAmountNotification ResponseXml
	Case "chargeback-amount-notification"
		ProcessChargebackAmountNotification ResponseXml
	Case "refund-amount-notification"
		ProcessRefundAmountNotification ResponseXml
	Case "authorization-amount-notification"
		ProcessAuthorizationAmountNotification ResponseXml
	Case "merchant-calculation-callback"
		ProcessMerchantCalculationCallback ResponseXml
	Case Else
End Select 

' Handle a new-order-notifiation message
Sub ProcessNewOrderNotification(ResponseXml)

	Dim MyNewOrder
	Set MyNewOrder = New NewOrderNotification
	MyNewOrder.ParseNotification ResponseXml

	' ********** How to access NewOrderNotification variables **********
	' ***************************** BEGIN ******************************

	Response.write MyNewOrder.TimeStamp & "<BR>"
	Response.write MyNewOrder.GoogleOrderNumber & "<BR>"
	Response.write MyNewOrder.ShippingName & "<BR>"
	Response.write MyNewOrder.ShippingCost & "<BR>"
	Response.write MyNewOrder.BuyerId & "<BR>"
	Response.write MyNewOrder.MerCalcSuccessful & "<BR>"
	Response.write MyNewOrder.FulfillmentOrderState & "<BR>"
	Response.write MyNewOrder.FinancialOrderState & "<BR>"
	Response.write MyNewOrder.AdjustmentTotal & "<BR>"
	Response.write MyNewOrder.TaxTotal & "<BR>"
	Response.write MyNewOrder.OrderTotal & "<BR>"
	Response.Write MyNewOrder.MarketingEmailAllowed & "<BR>"

	Dim MyItem
	For Each MyItem In MyNewOrder.ItemArr
		Response.Write MyItem.Name & "<BR>"
		Response.Write MyItem.Description & "<BR>"
		Response.Write MyItem.Quantity & "<BR>"
		Response.Write MyItem.UnitPrice & "<BR>"
		Response.Write MyItem.MerchantItemId & "<BR>"
		Response.Write MyItem.TaxTableSelector & "<BR>"

		If IsObject(MyItem.MerchantPrivateItemData) Then
			Dim MyPrivateItemData
			Set MyPrivateItemData = MyItem.MerchantPrivateItemData
			' Retrieve <color> and <weight> values
			Response.Write GetElementText(MyPrivateItemData, "color") & "<BR>"
			Response.Write GetElementText(MyPrivateItemData, "weight") & "<BR>"
			' Or get the raw <merchant-private-item-data> XML
			' Response.Write MyPrivateItemData.xml & "<BR>"
			Set MyPrivateItemData = Nothing
		End If
	Next

	If IsObject(MyNewOrder.MerchantPrivateData) Then
		Dim MyPrivateData
		Set MyPrivateData = MyNewOrder.MerchantPrivateData
		Response.Write GetElementText(MyPrivateData, "session-id") & "<BR>"
		' Or get the raw <merchant-private-data> XML
		Response.Write MyPrivateData.xml & "<BR>"
		Set MyPrivateData = Nothing
	End If

	Dim BillingAddress
	Set BillingAddress = MyNewOrder.BuyerBillingAddress
	Response.Write BillingAddress.ID & "<BR>"
	Response.Write BillingAddress.ContactName & "<BR>"
	Response.Write BillingAddress.CompanyName & "<BR>"
	Response.Write BillingAddress.Address1 & "<BR>"
	Response.Write BillingAddress.Address2 & "<BR>"
	Response.Write BillingAddress.City & "<BR>"
	Response.Write BillingAddress.Region & "<BR>"
	Response.Write BillingAddress.PostalCode & "<BR>"
	Response.Write BillingAddress.CountryCode & "<BR>"
	Response.Write BillingAddress.Email & "<BR>"
	Response.Write BillingAddress.Phone & "<BR>"
	Response.Write BillingAddress.Fax & "<BR>"
	Set BillingAddress = Nothing

	Dim ShippingAddress
	Set ShippingAddress = MyNewOrder.BuyerShippingAddress
	Response.Write ShippingAddress.ID & "<BR>"
	Response.Write ShippingAddress.ContactName & "<BR>"
	Response.Write ShippingAddress.CompanyName & "<BR>"
	Response.Write ShippingAddress.Address1 & "<BR>"
	Response.Write ShippingAddress.Address2 & "<BR>"
	Response.Write ShippingAddress.City & "<BR>"
	Response.Write ShippingAddress.Region & "<BR>"
	Response.Write ShippingAddress.PostalCode & "<BR>"
	Response.Write ShippingAddress.CountryCode & "<BR>"
	Response.Write ShippingAddress.Email & "<BR>"
	Response.Write ShippingAddress.Phone & "<BR>"
	Response.Write ShippingAddress.Fax & "<BR>"
	Set ShippingAddress = Nothing

	If MyNewOrder.CouponIndex >= 0 Then
		Dim MyCoupon
		For Each MyCoupon In MyNewOrder.CouponArr
			Response.Write MyCoupon.Code & "<BR>"
			Response.Write MyCoupon.AppliedAmount & "<BR>"
			Response.Write MyCoupon.CalculatedAmount & "<BR>"
			Response.Write MyCoupon.Message & "<BR>"
		Next
	End If

	If MyNewOrder.GiftCertIndex >= 0 Then
		Dim MyGiftCert
		For Each MyGiftCert In MyNewOrder.GiftCertArr
			Response.Write MyGiftCert.Code & "<BR>"
			Response.Write MyGiftCert.AppliedAmount & "<BR>"
			Response.Write MyGiftCert.CalculatedAmount & "<BR>"
			Response.Write MyGiftCert.Message & "<BR>"
		Next
	End If

	' ***************************** END ******************************

	SendAck
End Sub

' Handle a order-state-change-notification message
Sub ProcessOrderStateChangeNotification(ResponseXml)

	Dim MyOrderState
	Set MyOrderState = New OrderStateChangeNotification
	MyOrderState.ParseNotification ResponseXml

	' ******** How to access OrderStateChangeNotification variables ********
	' ****************************** BEGIN *********************************

	Response.Write MyOrderState.TimeStamp & "<BR>"
	Response.Write MyOrderState.GoogleOrderNumber & "<BR>"
	Response.Write MyOrderState.NewFulfillmentOrderState & "<BR>"
	Response.Write MyOrderState.NewFinancialOrderState & "<BR>"
	Response.Write MyOrderState.PreviousFulfillmentOrderState & "<BR>"
	Response.Write MyOrderState.PreviousFinancialOrderState & "<BR>"

	' ****************************** END *********************************

	Dim GoogleOrderNumber
	GoogleOrderNumber = MyOrderState.GoogleOrderNumber

	' Possible list of Order Processing commands

	' Financial Commands:

	' MyOrder.ChargeOrder GoogleOrderNumber, Amount
	' MyOrder.SendOrderCommand

	' MyOrder.RefundOrder GoogleOrderNumber, Reason, Amount, Comment
	' MyOrder.SendOrderCommand

	' MyOrder.CancelOrder GoogleOrderNumber, Reason, Comment
	' MyOrder.SendOrderCommand

	' MyOrder.AuthorizeOrder GoogleOrderNumber
	' MyOrder.SendOrderCommand

	' Fulfillment Commands:

	' MyOrder.ProcessOrder GoogleOrderNumber
	' MyOrder.SendOrderCommand

	' MyOrder.AddMerchantOrderNumber GoogleOrderNumber, MerchantOrderNumber
	' MyOrder.SendOrderCommand

	' MyOrder.DeliverOrder GoogleOrderNumber, Carrier, TrackingNumber, True
	' MyOrder.SendOrderCommand

	' MyOrder.AddTrackingData GoogleOrderNumber, Carrier, TrackingNumber
	' MyOrder.SendOrderCommand
	
	' MyOrder.SendBuyerMessage GoogleOrderNumber, Mesage, True
	' MyOrder.SendOrderCommand

	' Archiving Commands:

	' MyOrder.ArchiveOrder GoogleOrderNumber
	' MyOrder.SendOrderCommand

	' MyOrder.UnarchiveOrder GoogleOrderNumber
	' MyOrder.SendOrderCommand


	Select Case MyOrderState.NewFinancialOrderState
		Case "REVIEWING"

		Case "CHARGEABLE"
			' MyOrder.ProcessOrder GoogleOrderNumber
			' MyOrder.SendOrderCommand

			' MyOrder.AuthorizeOrder GoogleOrderNumber
			' MyOrder.SendOrderCommand

			' MyOrder.ChargeOrder GoogleOrderNumber, ""
			' MyOrder.SendOrderCommand

			' MyOrder.CancelOrder GoogleOrderNumber, Reason, Comment
			' MyOrder.SendOrderCommand

		Case "CHARGING"

		Case "CHARGED"
			' MyOrder.AddTrackingData GoogleOrderNumber, Carrier, TrackingNumber
			' MyOrder.SendOrderCommand

			' MyOrder.DeliverOrder GoogleOrderNumber, Carrier, TrackingNumber, True
			' MyOrder.SendOrderCommand

			' MyOrder.RefundOrder GoogleOrderNumber, Reason, Amount, Comment
			' MyOrder.SendOrderCommand

		Case "PAYMENT_DECLINED"
			' MyOrder.CancelOrder GoogleOrderNumber, Reason, Comment
			' MyOrder.SendOrderCommand

		Case "CANCELLED"

		Case "CANCELLED_BY_GOOGLE"
			' MyOrder.SendBuyerMessage GoogleOrderNumber, Mesage, true
			' MyOrder.SendOrderCommand

		Case Else
	End Select

	Select Case MyOrderState.NewFulfillmentOrderState
		Case "NEW"

		Case "PROCESSING"

		Case "DELIVERED"
			' MyOrder.ArchiveOrder GoogleOrderNumber
			' MyOrder.SendOrderCommand

		Case "WILL_NOT_DELIVER"

		Case Else
	End Select


	SendAck
End Sub

' Handle a risk-information-notification message
Sub ProcessRiskInformationNotification(ResponseXml)

	Dim MyRiskInfo
	Set MyRiskInfo = New RiskInformationNotification
	MyRiskInfo.ParseNotification ResponseXml

	' ******** How to access RiskInformationNotification variables *********
	' ****************************** BEGIN *********************************

	Response.Write MyRiskInfo.TimeStamp & "<BR>"
	Response.Write MyRiskInfo.GoogleOrderNumber & "<BR>"
	Response.Write MyRiskInfo.IpAddress & "<BR>"
	Response.Write MyRiskInfo.EligibleForProtection & "<BR>"
	Response.Write MyRiskInfo.AvsResponse & "<BR>"
	Response.Write MyRiskInfo.CvnResponse & "<BR>"
	Response.Write MyRiskInfo.PartialCC & "<BR>"
	Response.Write MyRiskInfo.BuyerAccountAge & "<BR>"

	Dim BillingAddress
	Set BillingAddress = MyRiskInfo.BillingAddress
	Response.Write BillingAddress.ID & "<BR>"
	Response.Write BillingAddress.ContactName & "<BR>"
	Response.Write BillingAddress.CompanyName & "<BR>"
	Response.Write BillingAddress.Address1 & "<BR>"
	Response.Write BillingAddress.Address2 & "<BR>"
	Response.Write BillingAddress.City & "<BR>"
	Response.Write BillingAddress.Region & "<BR>"
	Response.Write BillingAddress.PostalCode & "<BR>"
	Response.Write BillingAddress.CountryCode & "<BR>"
	Response.Write BillingAddress.Email & "<BR>"
	Response.Write BillingAddress.Phone & "<BR>"
	Response.Write BillingAddress.Fax & "<BR>"
	Set BillingAddress = Nothing

	' ****************************** END *********************************

	SendAck
End Sub

' Handle a charge-amount-notification message
Sub ProcessChargeAmountNotification(ResponseXml)

	Dim MyChargeAmountNot
	Set MyChargeAmountNot = New ChargeAmountNotification
	MyChargeAmountNot.ParseNotification ResponseXml

	' ********** How to access ChargeAmountNotification variables **********
	' ****************************** BEGIN *********************************
	Response.Write MyChargeAmountNot.TimeStamp & "<BR>"
	Response.Write MyChargeAmountNot.GoogleOrderNumber & "<BR>"
	Response.Write MyChargeAmountNot.LatestChargeAmount & "<BR>"
	Response.Write MyChargeAmountNot.TotalChargeAmount & "<BR>"
	' ****************************** END *********************************

	SendAck
End Sub

' Handle a chargeback-amount-notification message
Sub ProcessChargebackAmountNotification(ResponseXml)

	Dim MyChargebackAmountNot
	Set MyChargebackAmountNot = New ChargebackAmountNotification
	MyChargebackAmountNot.ParseNotification ResponseXml

	' ******* How to access ChargebackAmountNotification variables ********
	' ****************************** BEGIN ********************************
	Response.Write MyChargebackAmountNot.TimeStamp & "<BR>"
	Response.Write MyChargebackAmountNot.GoogleOrderNumber & "<BR>"
	Response.Write MyChargebackAmountNot.LatestChargebackAmount & "<BR>"
	Response.Write MyChargebackAmountNot.TotalChargebackAmount & "<BR>"
	' ****************************** END *********************************

	SendAck
End Sub

' Handle a refund-amount-notification message
Sub ProcessRefundAmountNotification(ResponseXml)

	Dim MyRefundAmountNot
	Set MyRefundAmountNot = New RefundAmountNotification
	MyRefundAmountNot.ParseNotification ResponseXml

	' ********* How to access RefundAmountNotification variables **********
	' ****************************** BEGIN ********************************
	Response.Write MyRefundAmountNot.TimeStamp & "<BR>"
	Response.Write MyRefundAmountNot.GoogleOrderNumber & "<BR>"
	Response.Write MyRefundAmountNot.LatestRefundAmount & "<BR>"
	Response.Write MyRefundAmountNot.TotalRefundAmount & "<BR>"
	' ****************************** END ********************************

	SendAck
End Sub

' Handle an authorization-amount-notification message
Sub ProcessAuthorizationAmountNotification(ResponseXml)

	Dim MyAuthAmountNot
	Set MyAuthAmountNot = New AuthorizationAmountNotification
	MyAuthAmountNot.ParseNotification ResponseXml

	' ****** How to access AuthorizationAmountNotification variables *******
	' ****************************** BEGIN *********************************
	Response.Write MyAuthAmountNot.TimeStamp & "<BR>"
	Response.Write MyAuthAmountNot.GoogleOrderNumber & "<BR>"
	Response.Write MyAuthAmountNot.AvsResponse & "<BR>"
	Response.Write MyAuthAmountNot.CvnResponse & "<BR>"
	Response.Write MyAuthAmountNot.AuthorizationAmount & "<BR>"
	Response.Write MyAuthAmountNot.AuthorizationExpirationDate & "<BR>"
	' ******************************* END *********************************

	SendAck
End Sub

' Acknowledge the notification by responding to Google Checkout 
' with <notification-acknowledgment>.
Public Sub SendAck()
	Dim XmlAcknowledgment
	XmlAcknowledgment = "<?xml version=""1.0"" encoding=""UTF-8""?><notification-acknowledgment xmlns=""http://checkout.google.com/schema/2"" />"
	Response.Write XmlAcknowledgment
	LogMessage XmlAcknowledgment
End Sub

' Handle merchant-calculation-callback message and 
' Respond with merchant-calculation-results message
Sub ProcessMerchantCalculationCallback(ResponseXml)

	Dim MyMerCalcCallback
	Set MyMerCalcCallback = New MerchantCalculationCallback
	MyMerCalcCallback.ParseNotification ResponseXml

	' ********* How to access MerchantCalculationCallback variables *********
	' ****************************** BEGIN **********************************
	Dim MyItem
	For Each MyItem In MyMerCalcCallback.ItemArr
		Response.Write MyItem.Name & "<BR>"
		Response.Write MyItem.Description & "<BR>"
		Response.Write MyItem.Quantity & "<BR>"
		Response.Write MyItem.UnitPrice & "<BR>"
		Response.Write MyItem.MerchantItemId & "<BR>"
		Response.Write MyItem.TaxTableSelector & "<BR>"

		If IsObject(MyItem.MerchantPrivateItemData) Then
			Dim MyPrivateItemData
			Set MyPrivateItemData = MyItem.MerchantPrivateItemData
			' Retrieve <color> and <weight> values
			Response.Write GetElementText(MyPrivateItemData, "color") & "<BR>"
			Response.Write GetElementText(MyPrivateItemData, "weight") & "<BR>"
			' Or get the raw <merchant-private-item-data> XML
			' Response.Write MyPrivateItemData.xml & "<BR>"
			Set MyPrivateItemData = Nothing
		End If
	Next

	If IsObject(MyMerCalcCallback.MerchantPrivateData) Then
		Dim MyPrivateData
		Set MyPrivateData = MyMerCalcCallback.MerchantPrivateData
		' Retrieve <session-id>
		Response.Write GetElementText(MyPrivateData, "session-id") & "<BR>"
		' Or get the raw <merchant-private-data> XML
		' Response.Write MyPrivateData.xml & "<BR>"
		Set MyPrivateData = Nothing
	End If

	Response.Write MyMerCalcCallback.CartExpirationDate & "<BR>"
	Response.Write MyMerCalcCallback.BuyerId & "<BR>"
	Response.Write MyMerCalcCallback.BuyerLanguage & "<BR>"
	Response.Write MyMerCalcCallback.Tax & "<BR>"

	If MyMerCalcCallback.AnonymousAddressIndex >= 0 Then
		Dim AnonymousAddress
		For Each AnonymousAddress In MyMerCalcCallback.AnonymousAddressArr
			Response.Write AnonymousAddress.ID & "<BR>"
			Response.Write AnonymousAddress.ContactName & "<BR>"
			Response.Write AnonymousAddress.CompanyName & "<BR>"
			Response.Write AnonymousAddress.Address1 & "<BR>"
			Response.Write AnonymousAddress.Address2 & "<BR>"
			Response.Write AnonymousAddress.City & "<BR>"
			Response.Write AnonymousAddress.Region & "<BR>"
			Response.Write AnonymousAddress.PostalCode & "<BR>"
			Response.Write AnonymousAddress.CountryCode & "<BR>"
			Response.Write AnonymousAddress.Email & "<BR>"
			Response.Write AnonymousAddress.Phone & "<BR>"
			Response.Write AnonymousAddress.Fax & "<BR>"
		Next
	End If

	If MyMerCalcCallback.MerchantCodeIndex >= 0 Then
		Dim MerchantCode
		For Each MerchantCode In MyMerCalcCallback.MerchantCodeArr
			Response.Write MerchantCode & "<BR>"
		Next
	End If

	If MyMerCalcCallback.ShippingMethodIndex >= 0 Then
		Dim ShippingMethod
		For Each ShippingMethod In MyMerCalcCallback.ShippingMethodArr
			Response.Write ShippingMethod & "<BR>"
		Next
	End If

	' ****************************** END **********************************

	' Construct <merchant-calculation-results>
	Dim MyMerCalcResults, MyResult, MyCoupon, MyGiftCert, MerchantCodeType
	Set MyMerCalcResults = New MerchantCalculationResults

	' Loop through each anonymous address from the callback
	For Each AnonymousAddress In MyMerCalcCallback.AnonymousAddressArr

		' Construct and add <result> into <merchant-calculation-results>

		' Loop through each merchant-calculated-shipping method
		If MyMerCalcCallback.ShippingMethodIndex >= 0 Then
			For Each ShippingMethod In MyMerCalcCallback.ShippingMethodArr
				Set MyResult = New Result

				' Get the shipping name, rate and whether it's shippable
				MyResult.ShippingName = ShippingMethod
				MyResult.ShippingRate = 8
				MyResult.Shippable = True

				' Anonymous-address ID
				MyResult.AddressId = AnonymousAddress.ID

				' Calculate merchnat-calculated tax
				If MyMerCalcCallback.Tax = "true" Then
					MyResult.TotalTax = 5
				End If

				' Validate coupons and gift certificates
				If MyMerCalcCallback.MerchantCodeIndex >= 0 Then
					For Each MerchantCode In MyMerCalcCallback.MerchantCodeArr

						' Determine whether the code is a Coupon or GiftCert
						MerchantCodeType = "Coupon"

						' Create Coupon result
						If MerchantCodeType = "Coupon" Then
							Set MyCoupon = New CouponResult
							MyCoupon.Code = MerchantCode
							MyCoupon.Valid = True
							MyCoupon.Amount = 10
							MyCoupon.Message = "Your coupon has been applied successfully."
							MyResult.AddCouponResult MyCoupon
							Set MyCoupon = Nothing

						' Create GiftCert result
						ElseIf MerchantCodeType = "GiftCert" Then
							Set MyGiftCert = New GiftCertResult
							MyGiftCert.Code = MerchantCode
							MyGiftCert.Valid = True
							MyGiftCert.Amount = 10
							MyGiftCert.Message = "You gift certificate has been applied successfully."
							MyResult.AddGiftCertResult MyGiftCert
							Set MyGiftCert = Nothing
						End If
					Next
				End If

				' Add this <result> to <merchant-calculation-results>
				MyMerCalcResults.AddResult MyResult
				Set MyResult = Nothing
			Next

		' No merchant-calculated-shipping methods
		Else

			Set MyResult = New Result

			' Specify whether it's shippable
			MyResult.Shippable = True
			
			' Anonymous-address ID
			MyResult.AddressId = AnonymousAddress.ID

			' Calculate merchnat-calculated tax
			If MyMerCalcCallback.Tax = "true" Then
				MyResult.TotalTax = 5.00
			End If

			' Validate coupons and gift certificates
			If MyMerCalcCallback.MerchantCodeIndex >= 0 Then
				For Each MerchantCode In MyMerCalcCallback.MerchantCodeArr

					' Determine whether the code is a Coupon or GiftCert
					MerchantCodeType = "Coupon"

					' Create Coupon result
					If MerchantCodeType = "Coupon" Then
						Set MyCoupon = New CouponResult
						MyCoupon.Code = MerchantCode
						MyCoupon.Valid = True
						MyCoupon.Amount = 10
						MyCoupon.Message = "Your coupon has been applied successfully."
						MyResult.AddCouponResult MyCoupon
						Set MyCoupon = Nothing

					' Create GiftCert result
					ElseIf MerchantCodeType = "GiftCert" Then
						Set MyGiftCert = New GiftCertResult
						MyGiftCert.Code = MerchantCode
						MyGiftCert.Valid = True
						MyGiftCert.Amount = 15
						MyGiftCert.Message = "You gift certificate has been applied successfully."
						MyResult.AddGiftCertResult MyGiftCert
						Set MyGiftCert = Nothing
					End If
				Next
			End If

			' Add this <result> to <merchant-calculation-results>
			MyMerCalcResults.AddResult MyResult
			Set MyResult = Nothing
		End If
	Next

	' Respond to Google with <merchant-calculation-results>
	Dim MyMerCalcResultsXml
	MyMerCalcResultsXml = MyMerCalcResults.GetXml
	Response.Write MyMerCalcResultsXml

	LogMessage MyMerCalcResultsXml

	Set MyMerCalcCallback = Nothing
	Set MyMerCalcResults = Nothing
End Sub


Set MyOrder = Nothing
%>